package cn.meizhu.org.service.impl;

import cn.meizhu.base.service.impl.BaseServiceImpl;
import cn.meizhu.org.domain.Employee;
import cn.meizhu.org.domain.Shop;
import cn.meizhu.org.mapper.EmployeeMapper;
import cn.meizhu.org.mapper.ShopMapper;
import cn.meizhu.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private JavaMailSender javaMailSender;

    @Override
    public void patchShopByIds(Long[] ids) {
        shopMapper.patchShopByIds(ids);
    }

    @Override
    public void settlement(Shop shop) {
        //保存employee 返回主键

        employeeMapper.save(shop.getAdmin());
        shopMapper.save(shop);

        //跟新employee中的shop_id字段
        shop.getAdmin().setShop_id(shop.getId());
        employeeMapper.update(shop.getAdmin());

    }

    @Override
    public void updateBystate(Long id) {
        shopMapper.updateBystate(id);

        Shop shop = findById(id);
        Long admin_id = shop.getAdmin_id();
        /*Employee employee = employeeMapper.findById(shop.getAdmin_id());
        employee.setState(1);
        employeeMapper.update(employee);*/
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            helper.setFrom("2234849383@qq.com");
            helper.setSubject("店铺审核");
            helper.setText("<a href=\"http://127.0.0.1:8080/employee/updateState/"+admin_id+"\">激活账号</a>",true);
            helper.setTo("m2234849383@163.com");
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void overrule(Long id) {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
            helper.setFrom("2234849383@qq.com");
            helper.setSubject("店铺审核");
            helper.setText("很抱歉，您的审核没有通过，还请辛苦重新提交审核申请");
            helper.setText("<a href=\"http://127.0.0.1:8083/#/ShopUpdate/?id="+id+"\">重新提交</a>",true);
            helper.setTo("m2234849383@163.com");
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}
